package com.cjack.frenchexcel.util;

import net.sf.json.JSONObject;
import org.dom4j.DocumentException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XmlUtil {
    private static final String PREFIX_XML = "<xml>";
    private static final String SUFFIX_XML = "</xml>";

    private static final String PREFIX_CDATA = "<![CDATA[";

    private static final String SUFFIX_CDATA = "]]>";

    public static String mapToXlm( Map<String, String> params){
        StringBuffer strbuff = new StringBuffer( PREFIX_XML);
        if (null != params) {
            for ( Map.Entry< String, String> entry : params.entrySet()) {
                strbuff.append("<").append(entry.getKey()).append(">");
                strbuff.append(PREFIX_CDATA);
                if (null != entry.getValue()) {
                    strbuff.append(entry.getValue());
                }
                strbuff.append(SUFFIX_CDATA);
                strbuff.append("</").append(entry.getKey()).append(">");
            }
        }
        return strbuff.append(SUFFIX_XML).toString();
    }

    /**
     * @description 将xml字符串转换成map
     * @param xml
     * @return Map
     */
    public static Map<String, String> xmlToMap( String xml) {
        Map<String, String> map = new HashMap<String, String>();
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(xml); // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
            List<Element> list = rootElt.elements();// 获取根节点下所有节点
            for (Element element : list) { // 遍历节点
                map.put(element.getName(), element.getText());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * @description 将xml字符串转换成json
     * @param xml
     * @return Map
     */
    public static JSONObject xmlToJson(String xml) {
        JSONObject json = new JSONObject();
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(xml); // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
            List<Element> list = rootElt.elements();// 获取根节点下所有节点
            for (Element element : list) { // 遍历节点
                json.put( element.getName(), element.getText());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    public static void main(String[] args) {
        String xml = "<xml><out_trade_no>Q3NL0000442503084402</out_trade_no>\n" +
                "<device_info>SN12345678</device_info>\n" +
                "<body><![CDATA[iPhone]]></body></xml>";
        Map<String, String> m = xmlToMap( xml);
        System.out.println( m);
        String x = mapToXlm( m);
        System.out.println( x);
        JSONObject j = xmlToJson( xml);
        System.out.println( j);
    }
}
